//
//  MallViewController.swift
//  Box
//
//  Created by mn on 2025/4/2.
//

import UIKit
import SnapKit

class MallViewController: BaseViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
    private var collectionView: UICollectionView!
//    let mallTypeView = MallTypeView()
    var goodsModels = [GoodsModel]()
    override func viewDidLoad() {
        super.viewDidLoad()
        setupCollectionView()
        nav.backButton.isHidden = true
        self.title = "购买商城"
        
//        getType()
    }
    
    private func setupCollectionView() {
//        view.addSubview(mallTypeView)
//        mallTypeView.snp.makeConstraints { make in
//            make.top.equalTo(nav.snp.bottom)
//            make.left.right.equalToSuperview()
//            make.height.equalTo(70)
//        }
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .vertical
        layout.minimumInteritemSpacing = 8
        layout.minimumLineSpacing = 8
        let totalPadding: CGFloat = 16 * 2 + 8 // 左右边距 + 1 个间距
        let itemWidth = (view.frame.width - totalPadding) / 2
        layout.itemSize = CGSize(width: itemWidth, height: itemWidth * 0.75 + 80)
        view.backgroundColor = .white
        collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
        collectionView.backgroundColor = .fromHex("#efefef")
        collectionView.dataSource = self
        collectionView.delegate = self
        collectionView.contentInsetAdjustmentBehavior = .never
        collectionView.registerCells([MallGoodsCell.self])
        view.addSubview(collectionView)
        
        collectionView.snp.makeConstraints { make in
            make.top.equalTo(nav.snp.bottom)
            make.left.right.bottom.equalToSuperview()
        }
        collectionView.contentInset = .zero
        
//        mallTypeView.selectType = {
//            [weak self] id in
//            self?.getGoods(id, clear: true)
//        }
        collectionView.addMJHeader {
            [weak self] in
            self?.getGoods("", clear: true)
        }
        collectionView.headerBeginRefreshing()
    }
    
    func getGoods(_ goodsTopTypeId: String, clear: Bool = false) {
        let api: API = goodsTopTypeId.isEmpty ? .getRecommendGoodsList : .getGoodsListByTopTypeId(goodsTopTypeId: goodsTopTypeId)
        
        APIService.request(api: api) {
            [weak self] (result, response) in
            self?.collectionView.endAllRefreshing()
            if result.success, let models: GoodsModelData = Decoder.data(response) {
                if clear {
                    self?.goodsModels.removeAll()
                }
                self?.goodsModels.append(contentsOf: models.data)
                self?.collectionView.reloadData()
            }
        }
    }
    
//    func getType() {
//        APIService.request(api: .getGoodsTypeList) {
//            [weak self] (result,response) in
//            if result.success,let types: MallTypeData = Decoder.data(response){
//                self?.mallTypeView.categories = [MallType(id: "", name: "推荐", imgUrl: "", status: 1)]+types.data
//            }
//        }
//    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        goodsModels.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell: MallGoodsCell = collectionView.cell(indexPath)
        cell.goodsModel = goodsModels[indexPath.row]
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        UIEdgeInsets(top: 10, left: 16, bottom: 0, right: 16)
    }
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
       let vc = GoodsDetailViewController(id: goodsModels[indexPath.row].id)
        navigationController?.push(vc)
    }

}
